/*
 * CTimerTaskQueue.h
 *
 *  Created on: 2024年8月21日
 *      Author: chuan
 */

#ifndef CTIMERTASKQUEUE_H_
#define CTIMERTASKQUEUE_H_

#include "TimerTaskQueue.h"
#include "TThread.h"
#include "TEvent.h"
#include <list>
#include <mutex>


class CTimerTaskQueue : public TimerTaskQueue, protected comn::Thread
{
public:
    static const uint32_t DEVIATION = 10;   /// 计时器误差, 毫秒.

public:
	CTimerTaskQueue();
	virtual ~CTimerTaskQueue();

    virtual bool open();

    /**
     * destroy queue
     */
    virtual void close();


    virtual bool isOpen();

    virtual bool addTimer(TimerID id, uint32_t ms, TimerHandler handler);

    virtual bool addElapse(TimerID id, uint32_t ms, TimerHandler handler);

    virtual bool cancel(TimerID id);

    virtual void clear();

public:

    struct TimerTask
    {
        TimerID	m_id;
        TimerHandler m_handler;
        uint32_t  m_interval;
        bool      m_isRepeat;
        int64_t   m_expire;

        TimerTask() :
            m_id(),
            m_handler(),
            m_interval(0),
            m_isRepeat(false),
            m_expire(0)
        {
        }

        TimerTask(const TimerID& id, TimerHandler handler, uint32_t interval, bool repeat) :
            m_id(id),
            m_handler(handler),
            m_interval(interval),
            m_isRepeat(repeat),
            m_expire(interval)
        {
        }

        int64_t updateExpire()
        {
            m_expire += m_interval;
            return m_expire;
        }

        int64_t updateExpire(int64_t timeBase)
        {
            m_expire = m_interval + timeBase;
            return m_expire;
        }

        bool operator < (const TimerTask& task) const
        {
            return (m_expire < task.m_expire);
        }

    };


protected:


protected:
    virtual int run();
    virtual void doStop();


    typedef std::list< TimerTask >  TimerTaskList;
    typedef TimerTaskList::iterator iterator;

    iterator find(const TimerID& id);
    bool remove(const TimerID& id);
    iterator findWithExpire(int64_t expire);

    int64_t getCurTime();
    bool add(TimerTask& task);
    void insertTask(TimerTask& task);
    bool runTask(TimerTask& task);

    bool timedwait(int64_t ms);
    int64_t checkExpire();

    bool checkoutFront(TimerTask& task, int64_t& timeout);

protected:

    TimerTaskList   m_taskList;
    std::mutex      m_mutex;
    comn::Event     m_event;

};

#endif /* CTIMERTASKQUEUE_H_ */
